//
//  XDChineseContentListViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDChineseContentListViewController.h"
#import "XDPracticeChineseListModel.h"
#import "XDPracticeViewController.h"
@interface XDChineseContentListViewController ()
/** 数据源 */
@property(nonatomic,strong) NSMutableArray *contentListModelArr;
@end

@implementation XDChineseContentListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
     [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
}


-(NSMutableArray*)contentListModelArr{
    
    if (!_contentListModelArr) {
        _contentListModelArr = [NSMutableArray array];
    }
    return _contentListModelArr;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentListModelArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    XDPracticeChineseListModel *model = self.contentListModelArr[indexPath.row];
    cell.textLabel.text = model.lessonName;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XDPracticeChineseListModel *model = self.contentListModelArr[indexPath.row];
    
    XDPracticeViewController *vc = [[XDPracticeViewController alloc]init];
    vc.title = self.title;
    
    if([self.title isEqualToString:@"单词练习"]){
        vc.practiceType = PracticeTypeEnglish;
    }else if([self.title isEqualToString:@"汉字练习"]){
        vc.practiceType = PracticeTypeChinese;
    }else{
        NSLog(@"单词匹配错误");
    }
    
    vc.lessonId = model.ID;
    vc.termId = self.termId;
    vc.unitId = self.unitId;
    vc.gradeId = self.gradeId;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)initNetworkingWithRefreshStatus:(RefreshState)status{
    
    NSString *url = BaseURL(@"exercise.do?action=getlessonlist");
    NSDictionary *dict = @{@"unitid":self.unitId};
    
    [MBProgressHUD showHUD];
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        [MBProgressHUD hideHUD];
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = responseObj;
        
        if (dictArr.count == 0) {
            [MBProgressHUD showError:XDNetWorkError];
            return ;
        }
        
        NSMutableArray *contentListModelArr = [XDPracticeChineseListModel mj_objectArrayWithKeyValuesArray:dictArr];
        self.contentListModelArr = contentListModelArr;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
        [MBProgressHUD hideHUD];
        NSLog(@"%@",error);
    }];
}



@end
